package com.ruoyi.report_tg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.DealHeh;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_tg.domain.bo.TdtrcdjlbBsInfoBo;
import com.ruoyi.report_tg.domain.vo.TdtrcdjlbBsInfoVo;
import com.ruoyi.report_tg.domain.TdtrcdjlbBsInfo;
import com.ruoyi.report_tg.mapper.TdtrcdjlbBsInfoMapper;
import com.ruoyi.report_tg.service.ITdtrcdjlbBsInfoService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 土的天然稠度记录Service业务层处理
 *
 * @author zyb
 * @date 2024-05-24
 */
@RequiredArgsConstructor
@Service
@Transactional
public class TdtrcdjlbBsInfoServiceImpl implements ITdtrcdjlbBsInfoService {

    private final TdtrcdjlbBsInfoMapper baseMapper;

    /**
     * 查询土的天然稠度记录
     */
    @Override
    @FIllBasicInfo
    public TdtrcdjlbBsInfoVo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }

    /**
     * 查询土的天然稠度记录列表
     */
    @Override
    public TableDataInfo<TdtrcdjlbBsInfoVo> queryPageList(TdtrcdjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TdtrcdjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<TdtrcdjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询土的天然稠度记录列表
     */
    @Override
    public List<TdtrcdjlbBsInfoVo> queryList(TdtrcdjlbBsInfoBo bo) {
        LambdaQueryWrapper<TdtrcdjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TdtrcdjlbBsInfo> buildQueryWrapper(TdtrcdjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TdtrcdjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, TdtrcdjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, TdtrcdjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), TdtrcdjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), TdtrcdjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), TdtrcdjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), TdtrcdjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), TdtrcdjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), TdtrcdjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), TdtrcdjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangpsj() != null, TdtrcdjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), TdtrcdjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), TdtrcdjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), TdtrcdjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), TdtrcdjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), TdtrcdjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), TdtrcdjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), TdtrcdjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getHeh11()), TdtrcdjlbBsInfo::getHeh11, bo.getHeh11());
        lqw.eq(bo.getHezl11() != null, TdtrcdjlbBsInfo::getHezl11, bo.getHezl11());
        lqw.eq(bo.getHejstzl11() != null, TdtrcdjlbBsInfo::getHejstzl11, bo.getHejstzl11());
        lqw.eq(bo.getHejgtzl11() != null, TdtrcdjlbBsInfo::getHejgtzl11, bo.getHejgtzl11());
        lqw.eq(bo.getShuifzl11() != null, TdtrcdjlbBsInfo::getShuifzl11, bo.getShuifzl11());
        lqw.eq(bo.getGantzl11() != null, TdtrcdjlbBsInfo::getGantzl11, bo.getGantzl11());
        lqw.eq(bo.getHansl11() != null, TdtrcdjlbBsInfo::getHansl11, bo.getHansl11());
        lqw.eq(StringUtils.isNotBlank(bo.getHeh12()), TdtrcdjlbBsInfo::getHeh12, bo.getHeh12());
        lqw.eq(bo.getHezl12() != null, TdtrcdjlbBsInfo::getHezl12, bo.getHezl12());
        lqw.eq(bo.getHejstzl12() != null, TdtrcdjlbBsInfo::getHejstzl12, bo.getHejstzl12());
        lqw.eq(bo.getHejgtzl12() != null, TdtrcdjlbBsInfo::getHejgtzl12, bo.getHejgtzl12());
        lqw.eq(bo.getShuifzl12() != null, TdtrcdjlbBsInfo::getShuifzl12, bo.getShuifzl12());
        lqw.eq(bo.getGantzl12() != null, TdtrcdjlbBsInfo::getGantzl12, bo.getGantzl12());
        lqw.eq(bo.getHansl12() != null, TdtrcdjlbBsInfo::getHansl12, bo.getHansl12());
        lqw.eq(bo.getHanslpjz1() != null, TdtrcdjlbBsInfo::getHanslpjz1, bo.getHanslpjz1());
        lqw.eq(bo.getYex() != null, TdtrcdjlbBsInfo::getYex, bo.getYex());
        lqw.eq(bo.getSux() != null, TdtrcdjlbBsInfo::getSux, bo.getSux());
        lqw.eq(bo.getSuxzs() != null, TdtrcdjlbBsInfo::getSuxzs, bo.getSuxzs());
        lqw.eq(bo.getTudtrcd() != null, TdtrcdjlbBsInfo::getTudtrcd, bo.getTudtrcd());
        return lqw;
    }

    /**
     * 新增土的天然稠度记录
     */
    @Override
    public Boolean insertByBo(TdtrcdjlbBsInfoBo bo) {
        TdtrcdjlbBsInfo add = BeanUtil.toBean(bo, TdtrcdjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改土的天然稠度记录
     */
    @Override
    @DealHeh
    public Boolean updateByBo(TdtrcdjlbBsInfoBo bo) {
        TdtrcdjlbBsInfo update = BeanUtil.toBean(bo, TdtrcdjlbBsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TdtrcdjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除土的天然稠度记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
